package yl.hs.bmipfull.service.flow.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.flow.IFlowUserTaskDao;
import yl.hs.bmipfull.daos.system.IGlobalSearchDao;
import yl.hs.bmipfull.daos.system.IPojoSvcDao;
import yl.hs.bmipfull.pojo.flow.FlowUserTaskEntity;
import yl.hs.bmipfull.pojo.system.GlobalSearchEntity;
import yl.hs.bmipfull.service.flow.IFlowUserTaskService;
import yl.hs.bmipfull.service.system.impl.SystemBaseService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.LoginUser;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 张凯
 * @apiNote 流程任务信息服务层接口实现
 * @since 2021年07月08日 16时53分56秒 <br/>
 */
@Service
public class FlowUserTaskServiceImpl extends SystemBaseService implements IFlowUserTaskService {

    @Autowired
    IFlowUserTaskDao dao;

    @Autowired
    IPojoSvcDao pojoSvcDao;

    @Autowired
    IGlobalSearchDao globalSearchDao;

    @Override
    public List<FlowUserTaskEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(FlowUserTaskEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<Map<String,Object>> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(FlowUserTaskEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<Map<String,Object>> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        var list=dao.queryPage(pageQuery.getQuery());
        List<Map<String,Object>> resultList=new ArrayList<>();
        for (var entity:list) {
            var map=entity.toMap();
            var object=pojoSvcDao.getObjectByTableAndSN(entity.getDataCode(), entity.getObjectSN(),"sn");
            map.put("object",object);
            resultList.add(map);
        }
        result.setData(resultList);
        return result;
    }


    @Override
    public FlowUserTaskEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveFlowUserTask(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        FlowUserTaskEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new FlowUserTaskEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(FlowUserTaskEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入流程任务信息信息", Tl.toJson(entity), FlowUserTaskEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新流程任务信息信息", Tl.toJson(entity), FlowUserTaskEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, FlowUserTaskEntity.class);
        }
        return result;
    }

    @Override
    public Map<String, Object> getDataInfoByDataSn(String dataSN) {
        GlobalSearchEntity globalSearchEntity = globalSearchDao.findByDataSN(dataSN);
        List<Map<String, Object>> resultList = null;
        if (globalSearchEntity != null) {
            List<String> str = new ArrayList<>();
            str.add(dataSN);
            resultList = pojoSvcDao.queryTableData(globalSearchEntity.getDataCode(), "sn", str);
        }
        if (resultList.size() > 0) {
            return resultList.get(0);
        }
        return null;
    }

    @Override
    public List<FlowUserTaskEntity> getUserTaskByObjectSN(String dataSN,String handlerUserSN) {
        return dao.getUserTaskByObjectSN(dataSN,handlerUserSN);
    }

    @Override
    public List<FlowUserTaskEntity> queryListByFlowNodeSNAndObjectSN(String flowNodeSN, String objectSN) {
        return dao.queryListByFlowNodeSNAndObjectSN(flowNodeSN,objectSN);
    }

    @Override
    public List<FlowUserTaskEntity> QueryAllByUserAction(String handlerUserSN, short taskState) {
        return dao.QueryAllByUserAction(handlerUserSN,taskState);
    }
}